import com.sun.javaws.IconUtil;

import java.util.List;

/*class ListNode {
    public int val;
    public ListNode next;
    public ListNode(int val) {
        this.val = val;
    }
}
public class MyLinKedList2 {
    public ListNode head;//表示当前链表的头
    public void createList() {
        ListNode listNode1 = new ListNode(12);
        ListNode listNode2 = new ListNode(23);
        ListNode listNode3 = new ListNode(23);
        ListNode listNode4 = new ListNode(45);
        ListNode listNode5 = new ListNode(56);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
        this.head = listNode1;
    }

    public void display() {
        ListNode cur = this.head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //删除链表中等于给定值 val 的所有节点
    public ListNode removeAllVal(int Val) {
        if (this.head == null) {
            return null;
        }
        ListNode cur = this.head.next;
        ListNode prev = this.head;//定义的新头节点，目的为了和cur节点链接。
        while (cur != null) {
            if (cur.val == Val) {
                prev.next = cur.next;
                cur = cur.next;
            } else {
                prev = cur;
                cur = cur.next;
            }
        }//最后埋头
        if (this.head.val == Val) {//上面的方法删除不了头节点
            this.head = this.head.next;
        }
        return this.head;
    }//从指点节头开始打印。

    public void display2(ListNode newHead) {
        ListNode cur = newHead;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //查找是否包含关键字k是否在单链表中
    public boolean contains(int key) {
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }//打印单链表的长度。

    public int size() {
        ListNode cur = this.head;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }//头插法

    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = this.head;
        this.head = node;
    }//尾插法

    public void addLast(int data) {
        ListNode node = new ListNode(data);
        ListNode cur = this.head;
        if (this.head == null) {
            this.head = node;
        } else {
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
            node.next = null;
        }
    }//找到index-1位置的节点的地址

    public ListNode findIndxt(int index) {
        ListNode cur = this.head;
        while (index - 1 != 0) {//index-1的目的是因为，第一个节点以下标0开始
            cur = cur.next;
            index--;
        }
        return cur;
    }//任意位置插入，第一个数据节点为0下标开始

    public void addIndex(int index, int data) {
        if (index < 0 || index > size()) {
            System.out.println("位置不合法");
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        ListNode cur = findIndxt(index);
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;
    }//找到要删除的关键字的前驱

    public ListNode searchPerv(int key) {
        ListNode cur = this.head;
        while (cur.next != null) {
            if (cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }//删除第一次出现关键字为key的节点

    public void remove(int key) {
        ListNode cur = this.head.next;
        ListNode prev = this.head;
        if (this.head == null) {
            System.out.println("此链表为空");
        }
        if (this.head.val == key) {
            this.head = prev.next;
        }
        while (cur != null) {
            if (cur.val == key) {
                prev.next = cur.next;
            }
            prev = prev.next;
            cur = cur.next;
        }
        if (cur == null) {
            System.out.println("没有要删除的节点");
        }
    }//清理单链表
    public void clean() {
        //粗暴的方达this.head=null;
        while (this.head != null) {
            ListNode newNext = this.head.next;
            this.head.next = null;
            this.head = newNext;
        }
    }//反转一个链表

    public ListNode reverseList() {
        ListNode cur = this.head;
        ListNode prev = null;
        if (this.head == null) {
            return null;
        }
        while (cur != null) {
            ListNode curNest = cur.next;
            cur.next = prev;
            prev = cur;
            cur = curNest;
        }
        this.head = prev;
        return prev;
    }//给定一个带有头结点 head 的非空单链表，返回链表的中间结点。如果有两个中间结点，则返回第二个中间结点

    public ListNode middleNode() {
        ListNode fast = this.head;
        ListNode slow = this.head;
        if (this.head == null) {
            return null;
        }
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;

    }//输入一个链表，输出该链表中倒数第k个结点

    public ListNode findKthToTail(int k) {
        if (k < 0 || head == null) {
            return null;
        }
        ListNode fast = this.head;
        ListNode slow = this.head;
        while (k - 1 != 0) {
            fast = fast.next;
            if (fast == null) {
                return null;
            }
            k--;
        }
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }//给一定值x，编写一段代码将所有小于x的结点排在其余结点之前，且不能改变原来的数据顺序

    public ListNode partition(int x) {
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.val < x) {
                //1.第一次
                if (bs == null) {
                    bs = cur;
                    be = cur;
                } else {//2.不是第一次尾插
                    be.next = cur;
                    be = be.next;
                }
            } else {
                if (as == null) {
                    as = cur;
                    ae = cur;
                } else {
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        if (be == null) {
            return as;
        }be.next=as;
        if (as != null) {
            ae.next = null;
        }
        return bs;
    }
    public boolean chkPalindrome(ListNode head){
        ListNode fast=this.head;
        ListNode slow=this.head;
        ListNode cur=this.head;
        while(fast!=null&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
        }cur = slow.next;
        while(cur!=null) {
            ListNode curNext = cur.next;
            cur.next = slow;//如果把cur.next的值赋给slow，那么前面的地址全部要改。
            slow = cur;
            cur = curNext;
        }
        while(head!=slow){//当节点是奇数的情况
            if(head.val!=slow.val){//判断值是否相等
                return false;
            }
            head=head.next;
            slow=slow.next;
            if(head.next==slow){//当节点是偶数的情况
                return true;
            }
        }return true;
        }
    }
*/